//
//  PhotoDetailViewController.m
//  auto
//
//  Created by 江玉元 on 14/11/6.
//  Copyright (c) 2014年 Ryo Aoyama. All rights reserved.
//

#import "PhotoDetailViewController.h"
#import "SlideImageView.h"
#import "NetHandler.h"
@interface PhotoDetailViewController ()<SlideImageViewDelegate>

@property (nonatomic, retain)SlideImageView *slideImageView;
@property (nonatomic, retain)NSMutableArray *photoArr;
@property (nonatomic, retain)NSMutableArray *titleArr;
@property (nonatomic, retain)UILabel *pageLabel;
@property (nonatomic, retain)UITextView *descLabel;
//@property (nonatomic, retain)NSString *pageCount;
@end

@implementation PhotoDetailViewController

- (void)dealloc
{
    [_slideImageView release];
    [_photoArr release];
    [_titleArr release];
    //[_pageCount release];
    [_pageLabel release];
    [_descLabel release];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    if (self) {
        CGRect rect = {{20,90},{250,220}};
        self.slideImageView = [[SlideImageView alloc] initWithFrame:rect ZMarginValue:5 XMarginValue:10 AngleValue:0.3 Alpha:1000];
        self.slideImageView.borderColor = [UIColor whiteColor];
        self.slideImageView.delegate = self;
    }
    return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.photoArr = [NSMutableArray array];
        self.titleArr = [NSMutableArray array];
    }
    return self;
}

- (void)startConnection
{
    NSString *url = [NSString stringWithFormat:@"http://agent1.pconline.com.cn:8941/pcautophoto/iphone_json.jsp?id=%@", self.photo.Id];
    [NetHandler networkWithURL:url completion:^(id result) {
        NSMutableArray *array = [result objectForKey:@"photos"];
        //self.pageCount = [result objectForKey:@"num"];
        for (NSDictionary *dic in array) {
            // 获取图片数组
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@"url"]]]];
            [self.photoArr addObject:image];
            
            // 获取标题数组
            NSString *titleStr = [dic objectForKey:@"desc"];
            if (![titleStr isEqualToString:@""]) {
                [self.titleArr addObject:titleStr];
            } else {
                [self.titleArr addObject:[dic objectForKey:@"name"]];
            }
        }
        
        
        for (int i = 0; i < self.photoArr.count; i++) {
            
            [self.slideImageView addImage:self.photoArr[i]];
        }
        
        [self.slideImageView setImageShadowsWtihDirectionX:2 Y:2 Alpha:0.7];
        self.descLabel.text = [self.titleArr firstObject];
        CGRect rect = [self.descLabel.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.f]} context:nil];
        self.descLabel.frame = CGRectMake(10, self.view.frame.size.height - 226, self.view.frame.size.width - 20, rect.size.height + 7);
        if (rect.size.height > 140) {
            self.descLabel.scrollEnabled = YES;
            self.descLabel.frame = CGRectMake(10, self.view.frame.size.height - 226, self.view.frame.size.width - 20, 170);
        } else{
            self.descLabel.scrollEnabled = NO;
            self.descLabel.frame = CGRectMake(10, self.view.frame.size.height - 226, self.view.frame.size.width - 20, rect.size.height + 30);
        }
        //self.pageCount = self.photo.count;
        self.pageLabel.text = [NSString stringWithFormat:@"1/%@", self.photo.count];
        
        [self.slideImageView reLoadUIview];

        
    }];
    
}

- (void)loadView
{
    [super loadView];
    [self.view addSubview:self.slideImageView];
    
    self.pageLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height - 40, 80, 30)];
    self.pageLabel.font = [UIFont boldSystemFontOfSize:17];
    self.pageLabel.textColor = [UIColor whiteColor];
    self.pageLabel.text = [NSString stringWithFormat:@"1/%@", self.photo.count];
    self.pageLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.pageLabel];
    [self.pageLabel release];
    
    self.descLabel = [[UITextView alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height - 226, self.view.frame.size.width - 20, 120)];
    self.descLabel.textColor = [UIColor whiteColor];
    self.descLabel.alpha = 0.8;
    self.descLabel.backgroundColor = [UIColor clearColor];
    //self.descLabel.backgroundColor = [UIColor whiteColor];
    self.descLabel.textAlignment = NSTextAlignmentNatural;
    self.descLabel.font = [UIFont boldSystemFontOfSize:15.f];
    self.descLabel.showsVerticalScrollIndicator = NO;
    self.descLabel.editable = NO;
    [self.descLabel sizeToFit];
    [self.view addSubview:self.descLabel];
    [self.descLabel release];
    
    // 下载按钮
    UIButton *download = [UIButton buttonWithType:UIButtonTypeSystem];
    download.frame = CGRectMake(145, self.view.frame.size.height - 40, 32, 32);
    UIImage *image = [UIImage imageNamed:@"download.png"];
    [download setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [download addTarget:self action:@selector(downloadAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:download];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"背景照片.JPG"]];
    bg.frame = self.view.bounds;
    [self.view addSubview:bg];
    [self.view sendSubviewToBack:bg];
    [bg release];
    //self.view.backgroundColor = [UIColor whiteColor];
    [self startConnection];
    
}

// 下载方法
- (void)downloadAction:(UIButton *)button
{
    UIImageWriteToSavedPhotosAlbum([self.photoArr objectAtIndex:(long)self.slideImageView.index], nil, nil, nil);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"照片已存储到相册中" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

#pragma mark 协议
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
//}

- (void)SlideImageViewDidScrollWithIndex:(int)index
{
    // 显示标题并自适应
    self.descLabel.text = [self.titleArr objectAtIndex:index];
    CGRect rect = [self.descLabel.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.f]} context:nil];
    if (rect.size.height > 130) {
        self.descLabel.scrollEnabled = YES;
        self.descLabel.frame = CGRectMake(10, self.view.frame.size.height - 226, self.view.frame.size.width - 20, 170);
    } else {
        self.descLabel.scrollEnabled = NO;
        self.descLabel.frame = CGRectMake(10, self.view.frame.size.height - 226, self.view.frame.size.width - 20, rect.size.height + 30);
    }
    self.pageLabel.text = [NSString stringWithFormat:@"%d/%@", index + 1, self.photo.count];

}



//- (void)SlideImageViewDidEndScorllWithIndex:(int)index
//{
//    //NSString *indexStr = [[NSString alloc] initWithFormat:@"当前为第%d/%@张", index + 1,];
//    //self.indexLabel.text = indexStr;
////    self.pageLabel.text = [NSString stringWithFormat:@"%d/%@", index + 1, self.pageCount];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
